/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.php;
import php.NativeArray;

class Lib { 

	public static function hashOfAssociativeArray<T>(arr : NativeArray, ?recursive:Bool = false) : Hash<T> {
		if ( arr == null ) return null;
		var result:Hash<T> = php.Lib.hashOfAssociativeArray(arr);
		if ( recursive ) {
			for ( k in result.keys() ) {
				var v:Dynamic = result.get(k);
				if ( isNativeArray(v) ) result.set(k, cast hashOfAssociativeArray(v, recursive));
			}
		}
		return result;
	}
	
	public static function associativeArrayOfHash<T>(h:Hash<T>, ?recursive:Bool=false) : NativeArray {
		if ( h == null ) return null;
		var result:NativeArray;
		if ( recursive ) {
			var tmp = new Hash<NativeArray>();
			for ( k in h.keys() ) {
				var v:Dynamic = h.get(k);
				if (Std.is(v, Hash)) tmp.set(k, associativeArrayOfHash(v, recursive));
				else tmp.set(k, v);
			}
			result = php.Lib.associativeArrayOfHash(tmp);
		}else {
			result = php.Lib.associativeArrayOfHash(h);
		}
		return result;
	}
	
	public static function isNativeArray(v:Dynamic):Bool {
		return untyped __php__("is_array($v)");
	}
}